#include "msgsocket.h"
#include "globalvars.h"

#include <QDataStream>
#include <QTextCodec>

MsgSocket::MsgSocket(QObject *parent) : QObject(parent)
{
    m_tcpSocket = new QTcpSocket(this);

    connect(m_tcpSocket, SIGNAL(readyRead()),
            this, SLOT(slotReadyRead()));

    m_tcpSocket->connectToHost("192.168.168.141", 8866);
}

void MsgSocket::parseUserMsg(QString msg)
{
    qDebug() << "MsgSocket::parseUserMsg";
    QStringList list = msg.split("#");
    int cmd = msg.at(0).toLatin1();
    switch (cmd) {
    case 'R': parseUserRegister(list.at(1)); break;    //用户注册
    case 'L': parseUserLogin(list.at(1)); break;       //用户登录
    case 'C': parseUserLogout(list.at(1)); break;      //用户退出
    case 'O': parseGetOnlineUser(list.at(1)); break;   //获取在线用户
    case 'A': parseGetMsgAll(list.at(1)); break;       //获取群消息
    case 'P': parseGetMsgPrivate(list.at(1)); break;   //获取私聊消息
    default:
        break;
    }
}

void MsgSocket::parseUserRegister(QString msg)
{
    qDebug() << "MsgSocket::parseUserRegister";
    int res = msg.at(0).toLatin1();
    if (res == RES_Down)
    {
        emit signalUserRegisterResult(true);
    }else
    {
        emit signalUserRegisterResult(false);
    }
}

void MsgSocket::parseUserLogin(QString msg)
{
    qDebug() << "MsgSocket::parseUserLogin";
    qDebug() << msg;
    QStringList list = msg.split("|");
    int res = msg.at(0).toLatin1();
    if (res == RES_Down)
    {
        GlobalVars::g_localUser.setID(list.at(1));
        GlobalVars::g_localUser.setName(list.at(2));
        GlobalVars::g_localUser.setPswd(list.at(3));
        emit signalUserLoginResult(true, msg.at(0));
    }else
    {
        emit signalUserLoginResult(false, list.at(1));
    }
}

void MsgSocket::parseUserLogout(QString msg)
{
    qDebug() << "MsgSocket::parseUserLogout";
    int res = msg.at(0).toLatin1();
    if (res == RES_Down)
    {
        emit signalUserLogoutResult(true);
    }else
    {
        emit signalUserLogoutResult(false);
    }
}

void MsgSocket::parseGetOnlineUser(QString msg)
{
    qDebug() << "parseGetOnlineUser(QString msg)";

    GlobalVars::g_onlineUserList->clear();
    QStringList list = msg.split("|");
    for (int i = 0; i < list.length() - 1; i += 3)
    {
        UserInfo info(list.at(i), list.at(i+1), list.at(i+2));
        GlobalVars::g_onlineUserList->append(info);
    }

    emit signalGetOnlineUserResult(true);
}

void MsgSocket::parseGetMsgAll(QString msg)
{
    qDebug() << "MsgSocket::parseGetMsgAll";
    QStringList list = msg.split("|");
    int res = msg.at(0).toLatin1();
    if (res)
    {
        emit signalGetMsgAllResult(list.at(1));
    }
}

void MsgSocket::parseGetMsgPrivate(QString msg)
{
    qDebug() << "MsgSocket::parseGetMsgPrivate";
    QStringList list = msg.split("|");
    int res = msg.at(0).toLatin1();
    if (res)
    {
        emit signalGetMsgPrivateResult(list.at(1), list.at(2));
    }
}

void MsgSocket::slotReadyRead()
{
    qDebug() << "void MsgSocket::slotReadyRead()";

    QString msg = m_tcpSocket->readAll();
    qDebug() << msg;
    parseUserMsg(msg);
}

void MsgSocket::slotSendMessage(QString msg)
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));

    char* ch;   
    QByteArray temp = msg.toLocal8Bit();
    ch = temp.data();

    qDebug() << "Client Send: " << ch;
    m_tcpSocket->write(ch);
}
